package xinxin.com.StrategyPattern.View;

import xinxin.com.StrategyPattern.Events.CalculateEvents;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class CalculateFrame {
    JFrame jFrame;

    JTextField tf1;
    JTextField tf2;
    JComboBox<String> favourableList;
    JTextArea ta;
    JLabel lb1;

    public CalculateFrame(String title) {
        jFrame = new JFrame(title);
        jFrame.setLayout(new GridLayout(3, 1, 4, 3));

        JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayout(3, 1, 4, 3));

        JPanel panel11 = new JPanel();
        panel11.add(new JLabel("单价:"));
        tf1 = new JTextField("0.00",20);
        tf1.addFocusListener(new FocusListener(){
            @Override
            public void focusLost(FocusEvent e) {
                //失去焦点执行的代码
                try {
                    Double.parseDouble(tf1.getText());
                } catch (NumberFormatException e1) {
                    tf1.setText("0.00");
                }
            }
            @Override
            public void focusGained(FocusEvent e) {
                //获得焦点执行的代码
                try {
                    if (!(Double.parseDouble(tf1.getText()) > 0)) {
                        tf1.setText("");
                    }
                } catch (NumberFormatException e1) {
                    tf1.setText("");
                }
            }
        });
        panel11.add(tf1);
        JButton btn1 = new JButton("计算");
        btn1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                double result = 0.0;
                double total = Double.valueOf(lb1.getText()).doubleValue();
                result = CalculateEvents.calculate(tf1.getText(), tf2.getText(), favourableList.getSelectedItem().toString());
                total += result;
                ta.setText(ta.getText() + "\n单价：" + tf1.getText() + " 数量：" + tf2.getText() + " " + favourableList.getSelectedItem().toString() + " 合计：" + result);
                tf1.setText("0.00");
                tf2.setText("0");
                lb1.setText(Double.toString(total));
            }
        });
        panel11.add(btn1);
        panel1.add(panel11);

        JPanel panel12 = new JPanel();
        panel12.add(new JLabel("数量:"));
        tf2 = new JTextField("0",20);
        tf2.addFocusListener(new FocusListener(){
            @Override
            public void focusLost(FocusEvent e) {
                //失去焦点执行的代码
                try {
                    Integer.parseInt(tf2.getText());
                } catch (NumberFormatException e1) {
                    tf2.setText("0");
                }
            }
            @Override
            public void focusGained(FocusEvent e) {
                //获得焦点执行的代码
                try {
                    if (!(Integer.parseInt(tf2.getText()) > 0)) {
                        tf2.setText("");
                    }
                } catch (NumberFormatException e1) {
                    tf2.setText("");
                }
            }
        });
        panel12.add(tf2);
        JButton btn2 = new JButton("重置");
        btn2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                tf1.setText("0.00");
                tf2.setText("0");
                ta.setText("");
                lb1.setText("0.00");
            }
        });
        panel12.add(btn2);
        panel1.add(panel12);

        JPanel panel13 = new JPanel();
        panel13.add(new JLabel("计算方式:"));
        String[] favourable = new String[]{"正常收费", "满300返100", "打八折"};
        favourableList = new JComboBox<>(favourable);
        panel13.add(favourableList);
        panel1.add(panel13);

        jFrame.add(panel1);

        JPanel panel2 = new JPanel();
        ta = new JTextArea(8, 30);
        panel2.add(ta);

        jFrame.add(panel2);

        JPanel panel3 = new JPanel();
        panel3.add(new JLabel("总价："));
        lb1 = new JLabel("0.00");
        panel3.add(lb1);

        jFrame.add(panel3);

        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.pack();
        jFrame.setVisible(true);
    }
}
